<?php
namespace tp5_lib\qq\utility;

class QQOAuth
{
    protected $appid="";
    protected $appKey="";
    protected $redirect_uri="";
    /**
     * @return self
     */
    static public function alloc($appid,$appKey,$redirect_uri)
    {
        $new=new self;
        $new->appid=$appid;
        $new->appKey=$appKey;
        $new->redirect_uri=$redirect_uri;
        return $new;
    }
    /**
     * 使用获取Authorization Code的方式进行验证
     * 返回值会作为get参数
     */
    public function getServerSideAuthUrl($scope,$state,$is_mobile)
    {
        $display = $is_mobile ? "mobile" : "";
        $redirect_uri=urlencode($this->redirect_uri);
        return "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={$this->appid}&redirect_uri=$redirect_uri&state=$state&scope=$scope&display=$display";
    }
    /**
     * 使用client-side的方式直接获取token
     * 返回值会拼接在URL的`#`后面
     */
    public function getClientSideAuthUrl($scope,$state,$is_mobile)
    {
        $display = $is_mobile ? "mobile" : "";
        $redirect_uri=urlencode($this->redirect_uri);
        return "https://graph.qq.com/oauth2.0/authorize?response_type=token&client_id={$this->appid}&redirect_uri=$redirect_uri&state=$state&scope=$scope&display=$display";
    }
    /**
     * @return array|false access_token,expires_in,refresh_token
     */
    public function getAccessToken($code)
    {
        $redirect_uri=urlencode($this->redirect_uri);
        $url="https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id={$this->appid}&client_secret={$this->appKey}&code=$code&redirect_uri=$redirect_uri";
        $response=$this->get($url);//access_token=4233D39110D24D501A034319D4D7E521&expires_in=7776000&refresh_token=B0F63379920CC3D0912D50BD979B68B8
        if(preg_match("/^callback\(/",$response)){
            $response=str_replace("callback( ","",$response);
            $response=str_replace(" );","",$response);
            $data = json_decode($response,true);
        }
        else{
            parse_str($response,$data);
        }
        if(isset($data["error"])){
            $this->error=$data["error"].":".$data["error_description"];
            return false;
        }
        return $data;
    }
    /**
     * @return array access_token,expires_in,refresh_token
     */
    public function refreshToken($refresh_token)
    {
        $url="https://graph.qq.com/oauth2.0/token?grant_type=refresh_token&client_id={$this->appid}&client_secret={$this->appKey}&refresh_token=$refresh_token";
        $response=$this->get($url);//access_token=CF02A4FDDB7439F64652AD96842D3173&expires_in=7776000&refresh_token=B0F63379920CC3D0912D50BD979B68B8
        parse_str($response,$data);
        return $data;
    }
    /**
     * @return array client_id,openid
     */
    public function getOpenid($access_token)
    {
        $url="https://graph.qq.com/oauth2.0/me?access_token=$access_token";
        $response=$this->get($url);//callback( {"client_id":"101552453","openid":"16F03E2E17BCB28F7D57060F74C06425"} );
        $response=str_replace("callback( ","",$response);
        $response=str_replace(" );","",$response);
        return json_decode($response,true);
    }
    /**
     * 获取unionid
     * 需要邮件开通应用打通功能才能用
     * @return array client_id,openid,unionid
     */
    public function getUnionid($access_token)
    {
        $url="https://graph.qq.com/oauth2.0/me?access_token=$access_token&unionid=1";
        $response=$this->get($url);
        $response=str_replace("callback( ","",$response);
        $response=str_replace(" );","",$response);
        return json_decode($response,true);
    }
    /**
{
    "ret": 0,
    "msg": "",
    "is_lost":0,
    "nickname": "空气巴巴授权号",
    "gender": "男",
    "province": "",
    "city": "",
    "year": "0",
    "constellation": "",
    "figureurl": "http:\/\/qzapp.qlogo.cn\/qzapp\/101552453\/16F03E2E17BCB28F7D57060F74C06425\/30",
    "figureurl_1": "http:\/\/qzapp.qlogo.cn\/qzapp\/101552453\/16F03E2E17BCB28F7D57060F74C06425\/50",
    "figureurl_2": "http:\/\/qzapp.qlogo.cn\/qzapp\/101552453\/16F03E2E17BCB28F7D57060F74C06425\/100",
    "figureurl_qq_1": "http://thirdqq.qlogo.cn/g?b=oidb&k=xuYnFeffI1ndv0MJQjV76g&s=40",
    "figureurl_qq_2": "http://thirdqq.qlogo.cn/g?b=oidb&k=xuYnFeffI1ndv0MJQjV76g&s=100",
    "figureurl_qq": "http://thirdqq.qlogo.cn/g?b=oidb&k=xuYnFeffI1ndv0MJQjV76g&s=100",
    "figureurl_type": "0",
    "is_yellow_vip": "0",
    "vip": "0",
    "yellow_vip_level": "0",
    "level": "0",
    "is_yellow_year_vip": "0"
}
     */
    public function getUserInfo($openid,$access_token)
    {
        $url="https://graph.qq.com/user/get_user_info?oauth_consumer_key={$this->appid}&openid=$openid&access_token=$access_token&format=json";
        $response=$this->get($url);
        return json_decode($response,true);
    }
    protected function get($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        if(!$output){
            $this->error="curl error:".curl_errno($ch)." message:".curl_error($ch);
            return false;
        }
        return $output;
    }
    protected $error="";
    public function getError()
    {
        return $this->error;
    }
}
